package com.qiaoanju.web.controller;

import com.qiaoanju.commons.enums.ResultCode;
import net.sf.json.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.Serializable;

/**
 * Created by zhangchong
 * on 2017/9/21.
 */
public class BaseController implements Serializable {

    public static Logger logger = LogManager.getLogger(BaseController.class.getName());

    /**
     *
     * @param response
     * @param code
     * @param message
     */
    public void printJson(HttpServletResponse response , ResultCode code , String message) {


        JSONObject json = new JSONObject();
        json.put("code", code.getValue());
        logger.info("**************************************************code: " + code);
        json.put("msg", message);

        // Print
        PrintWriter out;
        try {
            out = response.getWriter();
            out.print(json.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     *
     * @param response
     * @param code
     * @param json
     */
    public void printJson(HttpServletResponse response , ResultCode code ,JSONObject json ){

        //操作日志
        logger.info("**************************************************code: "+code);
        json.put("code", code.getValue());
        // Print
        PrintWriter out;
        try {
            out = response.getWriter();
            out.print(json.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     *
     * @param response
     * @param code
     * @param message
     */
    public void printJson(HttpServletResponse response , short code , String message) {


        JSONObject json = new JSONObject();
        json.put("code", code);
        logger.info("**************************************************code: " + code);
        json.put("msg", message);

        // Print
        PrintWriter out;
        try {
            out = response.getWriter();
            out.print(json.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
